import { selector } from 'recoil'
import {RootProivder, RootState, ConfigState, ConfigProvider} from './atom'
import { UserState, userInfoProvider } from './atom';
import { getStorage, setStorage } from '../storage';
import config from 'bin/local.config'

export const RootRender = selector<RootState>({
    key: 'rootRender',
    get: ({get}) => {
        const state = get(RootProivder)
        return state
    }
});

export const getConfigInfo = selector<ConfigState>({
    key: "configInfo",
    get: ({get}) => {
        const state = get(ConfigProvider)
        return {
            ...state,
            options: config
        }
    }
});

export const getUserInfo = selector<UserState>({
    key: "userInfo",
    get: ({get}) => {
        const userInfo = getStorage("authInfo")
        const state = get(userInfoProvider)
        return {...userInfo, ...state}
    },
    set: ({set}, data) => {
        set(userInfoProvider,data)
        setStorage("authInfo", data)
    }
})